import Product from '../entities/Product'

let readyProducts = {} as Record<string, { lock: boolean; product: Product }>

//添加就绪产品
export const addReadyProduct = (product: Product) => {
  readyProducts[product.id] = {
    lock: false,
    product
  }
}

//获取就绪产品
export const getReadyProduct = (id: string) => {
  //同时销毁掉那个已经获取的元素
  const productData = readyProducts[id]
  if (!productData || productData?.lock) {
    return null
  }
  productData.lock = true
  delete readyProducts[id]
  return productData.product
}

//清空就绪产品
export const clearReadyProducts = () => {
  readyProducts = {}
}
